package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 星河一叶Ck
 * @date 2024/10/6
 */

@Mapper
public interface AddressBookMapper {

    /**
     * 新增地址
     * @param addressBook
     */
    @Insert("insert into address_book(user_id, consignee, sex, phone, province_code, province_name, city_code, city_name, district_code, district_name, detail, label, is_default) VALUES " +
            "(#{userId},#{consignee},#{sex},#{phone},#{provinceCode},#{provinceName},#{cityCode},#{cityName},#{districtCode},#{districtName},#{detail},#{label},#{isDefault})")
    void insert(AddressBook addressBook);

    /**
     * 查询当前登录用户的所有地址信息
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id = #{userId}")
    List<AddressBook> list(Long userId);

    /**
     * 将所有地址设置为非默认
     * @param addressBook
     */
    @Update("update address_book set is_default = #{isDefault} where user_id = #{userId}")
    void updateIsDefaultByUserId(AddressBook addressBook);

    /**
     * 修改操作
     * @param addressBook
     */
    void update(AddressBook addressBook);

    /**
     * 根据id查询地址信息
     * @param id id是唯一标识在数据库 所以这里不用用户id也是可以的
     * @return
     */
    @Select("select * from address_book where id = #{id}")
    AddressBook getAddressById(Long id);

    /**
     * 查询默认地址
     * @param userId
     * @return
     */
    @Select("select * from address_book where is_default = 1 and user_id = #{userId}")
    AddressBook getDefaultAddressBook(Long userId);

    @Delete("delete from address_book where id = #{id}")
    void deleteById(Long id);
}
